import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/viewmodel/service_order/multi_choose_employee_view_model.dart';

class MultiChooseEmployeePage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _MultiChooseEmployeeState();
}

class _MultiChooseEmployeeState extends BaseMvvmListState<
    MultiChooseEmployeeViewModel, MultiChooseEmployeePage> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("选择员工"),
        actions: [
          CommonText('确定',textColor:Colours.app_main ).addToContainer(
            padding: EdgeInsets.symmetric(horizontal: 16),
            alignment: Alignment.center,
          ).onTap(() {
            if(viewModel.selectItems.isEmpty){
              showToast("请选择员工");
              return;
            }
            finish(result: viewModel.selectItems);
          })
        ],
      ),
      body: Column(
        children: [
          Container(
            child: CommonInput(
              hintText: "请输入房间号/姓名",
              border: InputBorder.none,
              isDense: true,
              padding: EdgeInsets.zero,
              textInputAction: TextInputAction.search,
              onChanged: (str) {
                viewModel.customer = str;
                if (str.isEmpty) {
                  viewModel.requestRefresh();
                }
              },
              needClear: true,
              onSubmitted: (str) {
                viewModel.requestRefresh();
              },
            ).addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 30.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10)),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              CheckWithText(defaultSelected:viewModel.selectItems.isNotEmpty&&viewModel.selectItems.length==viewModel.listItems.length ,title: (viewModel.selectItems.isNotEmpty&&viewModel.selectItems.length==viewModel.listItems.length)?"全不选":'全选', onChanged: (choosed){
                viewModel.allChoose(choosed);
              }),
              Container(width: 16,),
            ],
          ),
          loadingContentWidget.setWeight(1)
        ],
      ),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Row(
      children: [
        CommonText(item.userName).setWeight(1),
        CheckWithText(
            title: "",
            defaultSelected: viewModel.selectItems.contains(item),
            onChanged: (value) {
              viewModel.itemTap(index);
            })
      ],
    )
        .addToContainer(
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 15))
        .onTap(() {
      viewModel.itemTap(index);
    });
  }

  @override
  Widget get separatorDivider => Gaps.dividerLine;

  @override
  MultiChooseEmployeeViewModel createViewModel() {
    return MultiChooseEmployeeViewModel(context);
  }
}
